package me.firstapp.api.system;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

import me.firstapp.api.base.BaseApi;
import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.module.system.Statistics;
import me.firstapp.service.system.StatisticsService;

@Controller
@RequestMapping(value = "/statistics")
@Api(value = "/statistics", description = "统计相关接口")
public class StatisticsApi extends BaseApi {

	@Autowired
	private StatisticsService statisticsService;

	@RequestMapping(value = "findStatistics.do", method = RequestMethod.GET)
	@ApiOperation(value = "获取统计信息", notes = "获取统计信息")
	public void findStatistics(HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Statistics> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Statistics statistics = statisticsService.findStatisticsByClientId(apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(statistics);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

}
